package com.yjd.back.contentmanager;

import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.app.model.BannerModel;
import com.yjd.comm.app.service.IBannerService;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.ReferenceTypeEnum;
import com.yjd.comm.fs.util.FileUtil;
import com.yjd.comm.resource.model.ResourceModel;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 首页banner后台管理
 * Created by yangchangyan on 2017/7/7.
 */
@Controller("com.yjd.back.contentmanager.BannerControl")
@Scope("prototype")
@RequestMapping("/banner")
public class BannerControl extends BaseControl{

    @Resource
    private IBannerService bannerService;

    /**
     * 滚动条列表
     * @return void
     * @throws Exception
     */
    @RequestMapping(value = "/m/list")
    public String list() throws Exception{
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("order", " order by banner_type");
        List<BannerModel> banner = bannerService.getModelList(paramMap, DbWREnums.READ);
        request.setAttribute("banner", banner);
        return "banner.list";
    }

    /**
     * 更具banner_id更新banner
     * Created by yangchangyan on 2017/6/30.
     * @param banners 要修改的内容
     * @return void
     * @throws
     */
    @RequestMapping(value = "/m/update", method = RequestMethod.POST)
    public void update(BannerDto banners) throws Exception{
        List<BannerModel> bns = banners.getBanners();
        if(null == bns || bns.size() == 0){
            FrameUtil.throwBaseException1("参数错误");
        }
        for(BannerModel banner:bns){
            if(null == banner.getBanner_id() || banner.getBanner_id() < 0){
                continue;
            }
            bannerService.update(banner);
        }
        this.successMsg("更新成功");
    }

    @RequestMapping(value = "/m/toinsert")
    public String insert() throws Exception {
        return "banner.add";
    }

    /**
     * 添加banner
     * Created by yangchangyan on 2017/6/30.
     * @param banner 添加的banner内容
     * @return void
     * @throws
     */
    @RequestMapping(value = "/m/insert", method = RequestMethod.POST)
    public void insert(MultipartHttpServletRequest multipartRequest, BannerModel banner) throws Exception {
        if(null == banner){
            FrameUtil.throwBaseException1("参数错误");
        }
        if(StringUtils.isEmpty(banner.getTitle())){
            FrameUtil.throwBaseException1("请输入标题!");
        }

        try {
            Map<String, MultipartFile> mulFileMap = multipartRequest.getFileMap();

            ResourceModel res = new ResourceModel();
            res.setRef_type(ReferenceTypeEnum.BANNER.getCode());
            String imgPath = "";
            // 上传PC banner图片
            imgPath = FileUtil.uploadFileToRemote(mulFileMap.get("pc_imgfile"), ReferenceTypeEnum.BANNER, res);
            banner.setPc_image_path(imgPath);

            // 上传ANDROID banner图片
            imgPath = FileUtil.uploadFileToRemote(mulFileMap.get("android_imgfile"), ReferenceTypeEnum.BANNER, res);
            banner.setAndroid_image_path(imgPath);

            // 上传IOS banner图片
            imgPath = FileUtil.uploadFileToRemote(mulFileMap.get("ios_imgfile"), ReferenceTypeEnum.BANNER, res);
            banner.setIos_image_path(imgPath);

            bannerService.insert(banner);
        } catch (Exception e) {
            FrameUtil.throwBaseException1("新增失败,"+e.getMessage());
        }

        this.successMsg(ServletUtil.getUrl("/banner/m/list"),"添加banner成功");
    }

    /**
     * 删除指定banner
     * Created by yangchangyan on 2017/6/30.
     * @param banner_id 要删除的banner
     * @return void
     * @throws
     */
    @RequestMapping(value = "/m/delete")
    public void delete(@RequestParam(value = "banner_id") Long banner_id) throws Exception{
        bannerService.delete(banner_id);
        this.successMsg("删除成功");
    }

}
class BannerDto{
    private static final long serialVersionUID = 101L;
    private List<BannerModel> banners;

    public List<BannerModel> getBanners() {
        return banners;
    }

    public void setBanners(List<BannerModel> banners) {
        this.banners = banners;
    }
}